//
//  SISSLoginNetworkingManager.m
//  SISSAPP
//
//  Created by 优房网 on 2017/3/24.
//  Copyright © 2017年 优房网. All rights reserved.
//

#import "SISSLoginNetworkingManager.h"

@implementation SISSLoginNetworkingManager

+ (instancetype)sharedManager{
    static SISSLoginNetworkingManager *instance = nil;
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        // 实例化Manager
        instance = [[SISSLoginNetworkingManager alloc]initWithBaseURL:[NSURL URLWithString:BASE_URL]];
        
        instance.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/html", @"text/plain", nil];
        instance.requestSerializer = [AFJSONRequestSerializer serializer];
        instance.requestSerializer.timeoutInterval = 30;
        
    });
    if ([SISSUserAccount shared].userName.length > 0) {
        [instance.requestSerializer setValue:[SISSUserAccount shared].userName forHTTPHeaderField:@"username"];
        [instance.requestSerializer setValue:[SISSUserAccount shared].token forHTTPHeaderField:@"token"];
        [instance.requestSerializer setValue:[NSString stringWithFormat:@"%@",@([SISSUserAccount shared].sa_id)] forHTTPHeaderField:@"said"];
    }
    return instance;
}

- (void)GETWithURLString:(NSString *)URLString parameters:(NSDictionary *)parameters success:(void(^)(id responseObject))success faile:(void(^)(NSError *error))faile{
    
    if (![SISSPublicSingleton shared].isHud) {
        [MBProgressHUD YYR_ShowHUD:YES];
        [SISSPublicSingleton shared].isHud = YES;
    }
    
    [self GET:URLString parameters:parameters progress:nil success:^(NSURLSessionDataTask * task, NSDictionary * responseObject) {
        // AFN的回调,默认就是在主线程
        if (success) {
            success(responseObject);
        }
        [SISSPublicSingleton shared].isHud = YES;;
        [MBProgressHUD YYR_HideHUD:YES];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (faile) {
            faile(error);
        }
        [SISSPublicSingleton shared].isHud = YES;;
        [MBProgressHUD YYR_HideHUD:YES];
    }];
}

- (void)POSTWithURLString:(NSString *)URLString parameters:(id)parameters success:(void(^)(id responseObject))success faile:(void(^)(NSError *error))faile{
    
    if (![SISSPublicSingleton shared].isHud) {
        [MBProgressHUD YYR_ShowHUD:YES];
        [SISSPublicSingleton shared].isHud = YES;
    }
    
    [self POST:URLString parameters:parameters progress:nil success:^(NSURLSessionDataTask * task, NSDictionary * responseObject) {
        // AFN的回调,默认就是在主线程
       
        if (success) {
            success(responseObject);
        }
        [SISSPublicSingleton shared].isHud = NO;
        [MBProgressHUD YYR_HideHUD:YES];

    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (faile) {
            faile(error);
        }
        [SISSPublicSingleton shared].isHud = NO;;
        [MBProgressHUD YYR_HideHUD:YES];
    }];
}


- (void)uploadFilesWithURLStr:(NSString *)URLStr serverName:(NSString *)serverName imagesArray:(NSArray *)imagesArray parameters:(NSDictionary *)parameters success:(void (^)(id responseObject))successBlock failed:(void (^)(NSError * error))failedBlock {
    
    if (![SISSPublicSingleton shared].isHud) {
        [MBProgressHUD YYR_ShowHUD:YES];
        [SISSPublicSingleton shared].isHud = YES;
    }
    [[SISSLoginNetworkingManager sharedManager] POST:URLStr parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        
        NSUInteger i = 0 ;
        
        // 上传图片时，为了用户体验或是考虑到性能需要进行压缩
        for (UIImage *image in imagesArray) {
            
            // 压缩图片，指定宽度
            //            UIImage *  resizedImage =  [UIImage imageCompressed:image withdefineWidth:width];
            NSData * imgData = UIImageJPEGRepresentation(image, 0.5);
            NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
            formatter.dateFormat = @"yyyyMMddHHmmssSSS";
            NSString *str = [formatter stringFromDate:[NSDate date]];
            // 拼接文件路径
            NSString *fileName = [NSString stringWithFormat:@"%@%zd",str,i];
            // 拼接Data
            [formData appendPartWithFileData:imgData name:serverName fileName:fileName mimeType:@"application/octet-stream"];
            i++;
            //            [formData appendPartWithFileData:imgData name:[NSString stringWithFormat:@"picflie%ld",(long)i] fileName:@"image.png" mimeType:@" image/jpeg"];
            //            i--;
        }
        
    } progress:^(NSProgress * _Nonnull uploadProgress) {
        
        NSLog(@"downLoadProcess = %@",uploadProgress);
        //        if (uploadProgress) {
        //
        //            upLoadProgress(uploadProgress.completedUnitCount / uploadProgress.totalUnitCount);
        //        }
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSLog(@"responseObject = %@",responseObject);
        if (successBlock) {
            
            successBlock(responseObject);
        }
        [SISSPublicSingleton shared].isHud = YES;;
        [MBProgressHUD YYR_HideHUD:YES];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        NSLog(@"error = %@",error);
        if (failedBlock) {
            
            failedBlock(error);
        }
        [SISSPublicSingleton shared].isHud = YES;;
        [MBProgressHUD YYR_HideHUD:YES];
    }];
}

@end
